package com.zmn.brs.admin.controller.rule.engineer;


import com.alibaba.fastjson.JSONObject;
import com.zmn.brs.admin.controller.rule.base.AlarmRuleBaseController;
import com.zmn.brs.business.interfaces.rule.AlarmRuleBService;
import com.zmn.brs.model.bo.rule.CustomRuleBO;
import com.zmn.brs.model.entity.common.JsonItem;
import com.zmn.brs.model.entity.event.Event;
import com.zmn.brs.model.query.rule.EngineerRuleCustomQuery;
import com.zmn.brs.model.vo.rule.SalesRuleVO;
import com.zmn.brs.model.vo.rule.UnpaidRuleVO;
import com.zmn.brs.services.interfaces.event.EventService;
import com.zmn.brs.utils.JsonItemUtil;
import com.zmn.common.dto.VtDTO;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.mcc.common.dto.staff.StaffQueryDIO;
import com.zmn.mcc.common.dto.staff.StaffSimpleQueryDIO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;


/**
 * 描述： 工程师指标规则
 *
 * @author chongw
 * @since 2021-07-02 17:05:29
 */
@Controller
@RequestMapping("/brs/engineer/rule/")
public class EngineerRuleListController extends AlarmRuleBaseController {

    @Resource
    private EventService eventService;

    @Resource
    private AlarmRuleBService alarmRuleBService;

    /**
     * 收款未支付金额规则列表
     *
     * @param query
     * @author chongw
     * @since 2021-07-02 17:05:29
     */
    @RequestMapping("unpaid/list")
    public ModelAndView unpaidList(@ModelAttribute EngineerRuleCustomQuery query) {
        List<CustomRuleBO> items = this.getEngineerRuleList(query, UnpaidRuleVO.class);
        if (items != null) {
            for (CustomRuleBO ruleBO : items) {
                UnpaidRuleVO ruleVO = (UnpaidRuleVO) ruleBO.getData();
                ruleVO.setReceiverName(JsonItemUtil.getStaffJsonItemStr(ruleVO.getReceiverName()));
            }
        }
        Event event = eventService.findById(query.getEventId());
        ModelAndView mv = new ModelAndView("engineer/rule/unpaid/list");
        mv.addObject("data", event);
        mv.addObject("vo", query);
        mv.addObject("items", items);
        mv.addObject("sceneId", event.getSceneId());
        mv.addObject("eventId", event.getEventId());
        mv.addObject("flag", 2);
        return mv;
    }

    /**
     * 收款未支付金额规则列表
     *
     * @param query
     * @author chongw
     * @since 2021-07-02 17:05:29
     */
    @RequestMapping("unpaid/addEditView")
    public ModelAndView unpaidDetail(@ModelAttribute EngineerRuleCustomQuery query) {
        Event event = eventService.findById(query.getEventId());
        CustomRuleBO data = CustomRuleBO.builder()
                .data(UnpaidRuleVO.builder().build())
                .build();
        if (!NumberUtil.isNullOrZero(query.getThresholdId())) {
            List<CustomRuleBO> items = this.getEngineerRuleList(query, UnpaidRuleVO.class);
            if (!items.isEmpty()) {
                data = items.get(0);
                UnpaidRuleVO ruleVO = (UnpaidRuleVO) data.getData();
                if (!StringUtil.isBlank(ruleVO.getReceiverName())) {
                    if (!JsonItemUtil.isJosn(ruleVO.getReceiverName())) {
                        List<JsonItem> itemList = new ArrayList<>();
                        JsonItem jsonItem = JsonItem.builder()
                                .text(ruleVO.getReceiverName())
                                .value(ruleVO.getReceiverId()).build();
                        itemList.add(jsonItem);
                        ruleVO.setReceiverName(JSONObject.toJSONString(itemList));
                    }
                }
            }
        }
        ModelAndView mv = new ModelAndView("engineer/rule/unpaid/addEdit");
        mv.addObject("data", data);
        mv.addObject("eventId", event.getEventId());
        return mv;
    }

    /**
     * 派单客单价规则列表
     *
     * @param query
     * @author chongw
     * @since 2021-07-02 17:05:29
     */
    @RequestMapping("sales/list")
    public ModelAndView salesList(@ModelAttribute EngineerRuleCustomQuery query) {
        List<CustomRuleBO> items = this.getEngineerRuleList(query, SalesRuleVO.class);
        if (items != null) {
            for (CustomRuleBO ruleBO : items) {
                SalesRuleVO ruleVO = (SalesRuleVO) ruleBO.getData();
                ruleVO.setReceiverName(JsonItemUtil.getStaffJsonItemStr(ruleVO.getReceiverName()));
            }
        }
        Event event = eventService.findById(query.getEventId());
        ModelAndView mv = new ModelAndView("engineer/rule/sales/list");
        mv.addObject("data", event);
        mv.addObject("vo", query);
        mv.addObject("items", items);
        mv.addObject("sceneId", event.getSceneId());
        mv.addObject("eventId", event.getEventId());
        mv.addObject("flag", 2);
        return mv;
    }

    /**
     * 派单客单价详情
     *
     * @param query
     * @author chongw
     * @since 2021-07-02 17:05:29
     */
    @RequestMapping("sales/addEditView")
    public ModelAndView salesDetail(@ModelAttribute EngineerRuleCustomQuery query) {
        Event event = eventService.findById(query.getEventId());
        CustomRuleBO data = CustomRuleBO.builder()
                .data(SalesRuleVO.builder().build())
                .build();
        if (!NumberUtil.isNullOrZero(query.getThresholdId())) {
            List<CustomRuleBO> items = this.getEngineerRuleList(query, SalesRuleVO.class);
            if (!items.isEmpty()) {
                data = items.get(0);
                SalesRuleVO ruleVO = (SalesRuleVO) data.getData();
                if (!StringUtil.isBlank(ruleVO.getReceiverName())) {
                    if (!JsonItemUtil.isJosn(ruleVO.getReceiverName())) {
                        List<JsonItem> itemList = new ArrayList<>();
                        JsonItem jsonItem = JsonItem.builder()
                                .text(ruleVO.getReceiverName())
                                .value(ruleVO.getReceiverId()).build();
                        itemList.add(jsonItem);
                        ruleVO.setReceiverName(JSONObject.toJSONString(itemList));
                    }
                }
            }
        }
        ModelAndView mv = new ModelAndView("engineer/rule/sales/addEdit");
        mv.addObject("data", data);
        mv.addObject("eventId", event.getEventId());
        return mv;
    }

    /**
     * 获取工程师监控规则
     *
     * @param query
     * @return
     */
    protected List<CustomRuleBO> getEngineerRuleList(EngineerRuleCustomQuery query, Class clazz) {
        //设置监控周期
        if (StringUtil.isNotBlank(query.getTimeSearch())) {
            String[] timeRangeArray = query.getTimeSearch().split("-");
            query.setStartTime(timeRangeArray[0].trim() + " 00:00:00");
            query.setEndTime(timeRangeArray[1].trim() + " 23:59:59");
        }
        //查询工程师配置数据
        List<CustomRuleBO> items = Optional.ofNullable(alarmRuleBService.listPageRuleByEventQuery(query)).orElse(new ArrayList<>());
        items.stream().forEach(o -> {
            o.setData(JSONObject.parseObject(o.getThresholdValue(), clazz));
        });
        return items;
    }

    /**
     * 查询员工信息
     *
     * @param query
     * @return
     */
    @ResponseBody
    @RequestMapping("/listStaffVtBySimpleQueryDIO")
    public ResponseDTO<List<VtDTO>> listStaffVtBySimpleQueryDIO(@ModelAttribute StaffSimpleQueryDIO query) {
        ResponseDTO<List<VtDTO>> listResponseDTO = staffListRemoteService.listStaffVtBySimpleQueryDIO(query);
        return listResponseDTO;
    }
}
